using LitJson;
using UnityEngine;

namespace Framework.Save
{
    public static class JsonDataExt
    {
        public static float AsFloat(this JsonData data)
        {
            return (float)(double)data;
        }

        public static JsonData SetVector2(this JsonData data, string key, Vector2 value)
        {
            var vec = new JsonData();
            vec.SetJsonType(JsonType.Array);
            vec[0] = value.x;
            vec[1] = value.y;
            data[key] = vec;
            return data;
        }

        public static Vector2 GetVector2(this JsonData data, string key)
        {
            return data[key].AsVector2();
        }

        public static Vector2 AsVector2(this JsonData data)
        {
            return data.Count < 2 ? Vector2.zero : new Vector2(data[0].AsFloat(), data[1].AsFloat());
        }

        public static JsonData SetVector3(this JsonData data, string key, Vector3 value)
        {
            var vec = new JsonData();
            vec.SetJsonType(JsonType.Array);
            vec[0] = value.x;
            vec[1] = value.y;
            vec[2] = value.z;
            data[key] = vec;
            return data;
        }

        public static Vector3 GetVector3(this JsonData data, string key)
        {
            return data[key].AsVector3();
        }

        public static Vector3 AsVector3(this JsonData data)
        {
            return data.Count < 3 ? data.AsVector2() : new Vector3(data[0].AsFloat(), data[1].AsFloat(), data[2].AsFloat());
        }
    }
}